package pcgen.android.app;

import org.apache.commons.lang.Validate;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;

import pcgen.ClassUtils;
import pcgen.android.Logger;
import pcgen.android.ResourceUtils;
import pcgen.android.app.ApplicationEx;
import pcgen.android.app.ActivityEx;

public abstract class OptionMenusProvider 
{
	public boolean onCreateOptionsMenu(Activity activity, Menu menu)
	{
		Validate.notNull(activity);
		Validate.notNull(menu);
		
		try
		{
			AppUtils.inflateMenu(activity, menu, initializeOptionsMenuId());
			initializeOpenMenu(activity, menu);
			return true;
		}
		catch (Throwable tr)
		{
			Logger.e(TAG, "onCreateOptionsMenu", tr);
		}
    	
    	return false;
	}

	public boolean onOptionsItemSelected(Activity activity, MenuItem item)
	{
		Validate.notNull(activity);
		Validate.notNull(item);
		
    	try
    	{
    		String packageName = ClassUtils.getPackageName(activity.getClass());
    		
			if (item.getItemId() == android.R.id.home)
			{
	            // app icon in Action Bar clicked; go home
	            Intent intent = new Intent(activity, initializeHomeActivity());
	            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            activity.startActivity(intent);
				
				return true;
			}
			
    		if (item.getItemId() == initializeOptionsMenuAboutId())
	    	{
				AppUtils.alertWithAsset(activity, ResourceUtils.getString(activity, "dialog_title_about", packageName), initializeOptionsMenuAboutName(), null);
	    	}
    		else if (item.getItemId() == initializeOptionsMenuExitId())
	    	{
    			((ApplicationEx)activity.getApplication()).exit(activity, "");
    			
    			return true;
	    	}
	    	else if (item.getItemId() == initializeOptionsMenuHelpId())
	    	{
	    		if (activity instanceof ActivityEx)
	    		{
	    			ActivityEx activityEx = (ActivityEx)activity;
	    			
		    		Dialog dialog = new Dialog(activityEx);
		    		dialog.setContentView(initializeHelpViewLayoutId());
		    		dialog.setTitle(ResourceUtils.getString(activity, "dialog_title_help", packageName));
		    		dialog.setOwnerActivity(activityEx);
		    		
		    		android.widget.LinearLayout layout = (android.widget.LinearLayout)dialog.findViewById(initializeHelpLayoutId());
					android.util.DisplayMetrics dm = new android.util.DisplayMetrics(); 
		    		activityEx.getWindowManager().getDefaultDisplay().getMetrics(dm); 
		    		int height = dm.heightPixels; 
		    		int width = dm.widthPixels; 
		    		layout.getLayoutParams().height = height;
		    		layout.getLayoutParams().width = width;
		    		
		    		String data = ResourceUtils.getStringRaw(activityEx, activityEx.contextHelpId());
		    		WebView web = (WebView)dialog.findViewById(initializeHelpWebId());
		    		//web.setInitialScale(100);
		    		web.loadData(data, "text/html", "utf-8");
		    		
		    		dialog.show();
	    		}
				
				return true;
	    	}
    	}
		catch (Throwable tr)
		{
			Logger.e(TAG, "onOptionsItemSelected", tr);
		}
		
    	return false;
    }
	
	protected void initializeOpenMenu(Activity activity, Menu menu)
	{
	}

	@SuppressWarnings("rawtypes")
	protected abstract Class initializeHomeActivity();
	protected abstract int initializeHelpViewLayoutId();
	protected abstract int initializeHelpLayoutId();
	protected abstract int initializeHelpWebId();
	protected abstract int initializeOptionsMenuId();
	protected abstract int initializeOptionsMenuAboutId();
	protected String initializeOptionsMenuAboutName()
	{
		return "about.html";
	}
	protected abstract int initializeOptionsMenuExitId();
	protected abstract int initializeOptionsMenuHelpId();
	
	private static final String TAG = OptionMenusProvider.class.getSimpleName();
}
